/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.core.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.grayrabbit.pay.chinapay.ChinaPay;
import org.grayrabbit.pay.chinapay.common.PayConstant.CURRENCY_CODE;
import org.grayrabbit.pay.chinapay.common.PayConstant.PAY_TRADE_TYPE;
import org.grayrabbit.pay.chinapay.config.ChinaPayConfigUtil;
import org.grayrabbit.pay.chinapay.config.ChinaPayInfo;
import org.grayrabbit.pay.core.biz.manager.trade.ChinaPayTradeManager;
import org.grayrabbit.pay.core.model.common.SystemConstants;
import org.grayrabbit.pay.core.model.entity.transaction.Trade;
import org.grayrabbit.pay.core.model.enums.TradeEnum;
import org.grayrabbit.pay.core.model.exception.PayException;
import org.grayrabbit.pay.core.model.results.PayResult;
import org.grayrabbit.pay.trade.TradeChinaPayInfo;
import org.grayrabbit.pay.trade.TradeMoney;
import org.grayrabbit.web.struts2.GenericAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 接收交易信息结果
 * 
 * @author wy
 * @version $Id: TradeResultAction.java, v 0.1 2012-6-7 下午8:42:20 wy Exp $
 */
@Actions
public class PayTradeResultAction extends GenericAction {

    /**serialVersionUID  */
    private static final long   serialVersionUID = -1045457062589616620L;

    private static final Logger LOG              = LoggerFactory.getLogger(PayTradeResultAction.class);

    @Autowired
    private ChinaPayTradeManager        chinaPayTradeManager;

    private String              merid;
    private String              orderno;
    private String              transdate;
    private String              amount;
    private String              currencycode;
    private String              transtype;
    private String              status;
    private String              checkvalue;
    //这两变量命令跟我没关系，银联提供的就这样
    private String              GateId;
    private String              Priv1;

    /**
     * 此处直接抛出异常，银联接收到500错误，会再次发送
     */
    @Action("pay_trade_result")
    public String execute() {
    	try{
    		LOG.info("TRADE 接收订单号为{}的交易数据,交易状态{} ...", orderno, status);
            if (!ChinaPayConfigUtil.getChinaPayInfo().getMemId().equals(merid)) {
                //接收结果，抛出异常
                LOG.info("TRADE 商户号{}非本系统", merid);
                throw new PayException("ERROR_MERID", "商户号非本系统");
            }
            TradeChinaPayInfo chinaPayInfo = new TradeChinaPayInfo();
            chinaPayInfo.setCheckValue(checkvalue);
            chinaPayInfo.setCurrencyCode(CURRENCY_CODE.get(currencycode));
            chinaPayInfo.setOrderStatus(status);
            chinaPayInfo.setPayTradeType(PAY_TRADE_TYPE.get(transtype));
            chinaPayInfo.setTradeId(orderno);
            chinaPayInfo.setTransDate(transdate);
            chinaPayInfo.setMoney(new TradeMoney().convertFen(Long.valueOf(amount)));
            boolean flag = ChinaPay.verifyTransResponse(chinaPayInfo);
            if (!flag) {
                LOG.info("TRADE 接收订单号为{}的交易数据验证出错", orderno);
                throw new PayException("ERROR_TRADE", "订单号" + orderno + "交易验证出错");
            }
            final Trade trade = new Trade();
            //这里拿到的都是终结状态
            if (ChinaPayInfo.TRADRE_SUCCESS_CODE.equals(status)) {
                LOG.info("TRADE 接收订单号为{}的交易成功", orderno);
                trade.setStatusEnum(TradeEnum.PAY_SUCCESS);
            } else {
                LOG.info("TRADE 接收订单号为{}的交易失败", orderno);
                trade.setStatusEnum(TradeEnum.PAY_FAIL);
            }
            trade.setOrderTradeId(orderno);
            trade.setTradeAction(SystemConstants.ACTION_PAY);
            //trade.setMemo(Priv1);
            PayResult result = chinaPayTradeManager.updateChinaPay(trade);
            if (!result.isSuccess()) {
                LOG.info("TRADE 接收订单号为{}的交易后台处理出错,{}", orderno, result.getResultMsg());
                throw new PayException(result.getResultCode(), result.getResultMsg());
            }
            return renderText("接收成功");
    	}
    	catch (Exception e) {
    		LOG.info("TRADE 接收订单号为{}的交易后台处理出错", orderno,e);
            throw new PayException(e);
		}
        
    }

    public String getMerid() {
        return merid;
    }

    public void setMerid(String merid) {
        this.merid = merid;
    }

    public String getOrderno() {
        return orderno;
    }

    public void setOrderno(String orderno) {
        this.orderno = orderno;
    }

    public String getTransdate() {
        return transdate;
    }

    public void setTransdate(String transdate) {
        this.transdate = transdate;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getCurrencycode() {
        return currencycode;
    }

    public void setCurrencycode(String currencycode) {
        this.currencycode = currencycode;
    }

    public String getTranstype() {
        return transtype;
    }

    public void setTranstype(String transtype) {
        this.transtype = transtype;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCheckvalue() {
        return checkvalue;
    }

    public void setCheckvalue(String checkvalue) {
        this.checkvalue = checkvalue;
    }

    public String getGateId() {
        return GateId;
    }

    public void setGateId(String gateId) {
        GateId = gateId;
    }

    public String getPriv1() {
        return Priv1;
    }

    public void setPriv1(String priv1) {
        Priv1 = priv1;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}
